var criteria = 
{ Name: "Company Info", Level2: [
    { Name: "Business overview", Level3: [
		{ Name: "Subindustry", Level4: [{ Name: "Subindustry", CriteriaEntityField: "Subindustry", IsScreenable: true, CriteriaType: 3, HasFullRangeChart:false, HasOwnValue:false }] }
    ]}
]};

function uuid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
    }
    return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}

function HexToBase64(hex) {
    var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64 = "";
    var group;
    for (var i = 0; i < 30; i += 6) {
        group = parseInt(hex.substr(i, 6), 16);
        base64 += base64Digits[(group >> 18) & 0x3f];
        base64 += base64Digits[(group >> 12) & 0x3f];
        base64 += base64Digits[(group >> 6) & 0x3f];
        base64 += base64Digits[group & 0x3f];
    }
    group = parseInt(hex.substr(30, 2), 16);
    base64 += base64Digits[(group >> 2) & 0x3f];
    base64 += base64Digits[(group << 4) & 0x3f];
    base64 += "==";
    return base64;
}

function NUUID(uuid) {
    var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
    var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
    var b = hex.substr(10, 2) + hex.substr(8, 2);
    var c = hex.substr(14, 2) + hex.substr(12, 2);
    var d = hex.substr(16, 16);
    hex = a + b + c + d;
    var base64 = HexToBase64(hex);
    return new BinData(3, base64);
}

function processLevel1(criteria) {
    if (!db.CriteriaLevel1.findOne({ Name: criteria.Name })) {
        db.CriteriaLevel1.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            ShortName: 'CompInfo',
            Order: NumberInt(10)
        });
    }

    var criteriaLevel1Id = db.CriteriaLevel1.findOne({ Name: criteria.Name })._id;

    criteria.Level2.forEach(function (criteria, index) {
        processLevel2(criteriaLevel1Id, criteria, index);
    });
}

function processLevel2(criteriaLevel1Id, criteria, index) {
    if (!db.CriteriaLevel2.findOne({ Name: criteria.Name })) {
        db.CriteriaLevel2.insert({
            _id: NUUID(uuid()),
            CriteriaLevel1Id: criteriaLevel1Id,
            UniverseFeature: NumberInt(2),
            Name: criteria.Name,
            ShortName: 'Business',
            Order: NumberInt(1)
        });
    }

    var criteriaLevel2Id = db.CriteriaLevel2.findOne({ Name: criteria.Name })._id;
    var length = criteria.Level3.length;

    criteria.Level3.forEach(function (criteria, index) {
        processLevel3(criteriaLevel2Id, criteria, index+1);
    });
}

function processLevel3(criteriaLevel2Id, criteria, index) {
    if (!db.CriteriaLevel3.findOne({ Name: criteria.Name, CriteriaLevel2Id: criteriaLevel2Id })) 
    {
		var orderCompanyName = db.CriteriaLevel3.findOne({Name:"Company Name"}).Order;
		
        db.CriteriaLevel3.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            ShortName: "",
            Order: orderCompanyName,
            CriteriaLevel2Id: criteriaLevel2Id,
            Category: "None",
			isActiveCategory:true
        });
    }

    var criteriaLevel3Id = db.CriteriaLevel3.findOne({ Name: criteria.Name, CriteriaLevel2Id: criteriaLevel2Id })._id;
    
    criteria.Level4.forEach(function (criteria) {
        processLevel4(criteriaLevel2Id, criteriaLevel3Id, criteria, index);
    });
}

function processLevel4(criteriaLevel2Id, criteriaLevel3Id, criteria, index) {
    if (!db.TemplateCriteria.findOne({ Name: criteria.Name, CriteriaLevel2Id: criteriaLevel2Id })) {
        db.TemplateCriteria.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
			CriteriaEntity:null,
            CriteriaEntityField: criteria.CriteriaEntityField,
            CriteriaType: NumberInt(criteria.CriteriaType),
            CriteriaLevel2Id: criteriaLevel2Id,
            CriteriaLevel3Id: criteriaLevel3Id,
            IsScreenable: criteria.IsScreenable,
            ResearchProducts: null,
            HasFullRangeChart: criteria.HasFullRangeChart==false? false : true,
			HasOwnValue: criteria.HasOwnValue==false? false : true
       });
    }
}

processLevel1(criteria);    